<?php

class FrontendModel extends CI_Model {
	
	function __construct() 
	{
		parent::__construct();
	}

	/**
	 * Get all threads by a specific tag
	 * @param string $tag
	 * @param int $start
	 * @param int $range
	 */
	function getThreadsByTag($tag = null, $range = null, $start = null)
	{
		$this->db->order_by('bump_date','desc');
		if (!empty($tag)) {
			// Filter by a tag
			$query = $this->db->query("SELECT * FROM thread WHERE id IN ( SELECT thread_id FROM tag_cloud WHERE tag = '" . $tag ."') LIMIT ".$start.", ".$range.";");
		} else {
			// No tag
			$query = $this->db->get('thread', $range, $start);
		}
		$threads = $query->result();
		
		return $threads;
	}
	
	/**
	 * Get number of threads result in a search
	 * @param string $tag
	 */
	function getNoThreads($tag = null)
	{
		if (!empty($tag)) {
			$query = $this->db->query("SELECT * FROM thread WHERE id IN ( SELECT thread_id FROM tag_cloud WHERE tag = '" . $tag ."');");	
		} else {
			$query = $this->db->get('thread');
		}
		
		$noThreads = $query->result();
		
		return $noThreads;
	}
	
	/**
	 * Get newest thread
	 */
	function getNewestThread()
	{
		// Get new thread's id
		$this->db->order_by('id', 'desc');
		$this->db->limit(1);
		$query = $this->db->get('thread');
		
		return $query->row();
	}
	
	/** 
	 * Get a specific thread
	 * @param int $id
	 */
	function getThread($id)
	{
		$query = $this->db->get_where('thread', array('id' => $id));
		$topic = null;
		$thread = $query->row();
		
		return $thread;
	}
	
	/**
	 * Create new thread
	 * @param string $title
	 * @param string $content
	 */
	function createThread($title, $content)
	{
		// Create new thread
		$data = array(
			'title'   => $title,
			'content' => $content,
			'author'  => $_SESSION['display_name'],
			'user_id' => $_SESSION['user_id'],
			'group'   => $_SESSION['group']
		);				
		// Set bump date to NOW() with string escape
		$this->db->set('bump_date','NOW()', false);
		$this->db->insert('thread', $data);
	}
	
	/**
	 * Delete a thread
	 * @param int $id
	 */
	function deleteThread($id)
	{
		$this->db->delete('thread', array('id' => $id));
		$this->db->delete('tag_cloud', array('thread_id' => $id));
	}
	
	/**
	 * Save bump date
	 * @param int $id - thread id
	 */
	function saveBumpDate($id)
	{
		// Set bump date to NOW() with string escape
		$this->db->set('bump_date','NOW()', false);
		$this->db->where(array('id' => $id));
		$this->db->update('thread');
	}
	
	/**
	 * Get all post of a thread
	 * @param int $id
	 */
	function getThreadPosts($id)
	{
		$this->db->order_by('created_date');
		$query = $this->db->get_where('post', array('thread_id' => $id));
		$post = $query->result();
		
		return $post;
	}
	
	/**
	 * Create new post
	 * @param string $thread_id
	 * @param string $content
	 */
	function createPost($thread_id, $content)
	{	
		$data = array(
			'thread_id' => $thread_id,
			'content' => $content,
			'author'  => $_SESSION['display_name'],
			'user_id' => $_SESSION['user_id'],
			'group'   => $_SESSION['group']	
		);
		
		$this->db->insert('post', $data);
	}
	
	/**
	 * Delete a post
	 * @param int $id
	 */
	function deletePost($id)
	{
		$this->db->delete('post', array('id' => $id));
	}
	
	/**
	 * Get tags of a specific thread
	 * @param $id - thread id
	 */
	function getThreadTags($id)
	{
		$query = $this->db->get_where('tag_cloud', array('thread_id' => $id));
		$tags = $query->result();
		
		return $tags;
	}
	
	/** 
	 * Save tags
	 * Delete all old tags and update with new ones
	 * @param $id - thread id
	 * @param $tags
	 */
	function saveTags($id, $tags)
	{
		// Delete old tags of thread
		$this->db->delete('tag_cloud', array('thread_id' => $id));
		
		// Insert new tags to tag_cloud table
		$tagsList = explode(',', $tags);
		foreach ($tagsList as $tag) { 
			$data = array(
				'thread_id' => $id,
				'tag' => trim($tag)
			);
			$this->db->insert('tag_cloud', $data);
		}
	}
}